package com.kehutong.charge.admin;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.util.Objects;
import com.kehutong.charge.entity.ChargeArrears;
import com.kehutong.charge.entity.ChargeArrearsConfig;
import com.kehutong.charge.entity.ChargeFeeConfig;
import com.kehutong.charge.service.ChargeArrearsService;
import com.kehutong.charge.vo.FeeMsgDetailVO;
import com.kehutong.common.DeptService;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;

/**
 * @program: kht
 * @description: 欠费管理
 * @author: joneron
 * @create: 2021/6/23
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/arrears")
public class ChargeArrearsController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeArrearsController.class);

    @Inject
    DeptService deptService;
    @Inject
    ChargeArrearsService chargeArrearsService;



    /**
     * 欠费设置
     * @param entity
     * @param token
     * @return
     * @throws Exception
     */
    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:arrears:edit")
    @ReqMapping("/config/save")
    public Object configSave(ChargeArrearsConfig entity, Token token) throws Exception {
        return chargeArrearsService.configSave(entity,token);
    }


    /**
     * 欠费设置编辑
     * @param entity
     * @return
     */
    @Bean(copy=true)
    @HasPermission("pay:charge:arrears:edit")
    @ReqMapping("/config/update")
    public Object configUpdate(ChargeArrearsConfig entity){
        return chargeArrearsService.configUpdate(entity);
    }


    /**
     * 欠费设置删除
     * @param entity
     * @return
     */
    @HasPermission("pay:charge:arrears:edit")
    @ReqMapping("/config/delete")
    public Object configDelete(ChargeArrearsConfig entity) {
        return chargeArrearsService.configDeleteById(entity.getId());
    }

    /**
     * 欠费设置分页查询
     * @param jsonObject
     * @return
     */
    @ReqMapping("/config/page")
    @HasPermission("pay:charge:arrears:view")
    public Object configPage(JSONObject jsonObject){
        return chargeArrearsService.configPage(jsonObject);
    }



    @ReqMapping("/page")
    @HasPermission("pay:charge:arrears:view")
    public Object page(JSONObject jsonObject){
        return chargeArrearsService.findCommunityPage(jsonObject);
    }


    /**
     * 用户欠费列表导出
     * @param req
     * @param resp
     * @param jsonObject
     */
    @ReqMapping("/page/export")
    @HasPermission("pay:charge:arrears:view")
    public void pageExport(HttpServletRequest req, HttpServletResponse resp,JSONObject jsonObject){
        chargeArrearsService.pageExport(req,resp,jsonObject);
    }


    @ReqMapping("/detail/page")
    @HasPermission("pay:charge:arrears:view")
    public Object detailPage(JSONObject jsonObject){
        return chargeArrearsService.findDetailPage(jsonObject);
    }


    @ReqMapping("/list")
    @HasPermission("pay:charge:arrears:view")
    public Object list(JSONObject jsonObject) {
        return chargeArrearsService.findList(jsonObject);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:arrears:edit")
    @ReqMapping("/save")
    public Object save(ChargeArrears entity, Token token) throws Exception {
        List<String> departList = token.getDepartments();
        entity.setCreateName(token.getUsername());
        if(!Objects.isEmpty(departList)){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());
            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        return chargeArrearsService.save(entity);
    }


    @Bean(copy=true)
    @HasPermission("pay:charge:arrears:edit")
    @ReqMapping("/update")
    public Object update(ChargeArrears entity) throws Exception {

        return chargeArrearsService.update(entity);
    }


    @HasPermission("pay:charge:arrears:edit")
    @ReqMapping("/delete")
    public Object delete(ChargeArrears entity) {

        return chargeArrearsService.deleteById(entity.getId());
    }


    @ReqMapping("/get")
    public Object get(ChargeFeeConfig entity) {

        return Result.success(entity);
    }


    @ReqMapping("/setStatus")
    @HasPermission("pay:charge:arrears:edit")
    public Object setStatus(ChargeArrears entity) throws Exception {
        entity.setStatus(!entity.isStatus());
        chargeArrearsService.update(entity);
        return Result.success();
    }


    /**
     * 欠费统计分页功能
     */
    @ReqMapping("/statist/page")
    @HasPermission("pay:charge:arrears:view")
    public Object statistPage(JSONObject jsonObject) {
        return chargeArrearsService.statistPage(jsonObject);
    }


    /**
     * 欠费统计列表导出
     * @param req
     * @param resp
     * @param jsonObject
     */
    @ReqMapping("/statist/page/export")
    @HasPermission("pay:charge:arrears:view")
    public void statistPageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject){
        chargeArrearsService.statistPageExport(req,resp,jsonObject);
    }


    @ReqMapping("/fee/msg/detail")
    public FeeMsgDetailVO getFeeMsgDetail(JSONObject jsonObject) throws Exception{
        return chargeArrearsService.getFeeMsgDetail(jsonObject);
    }

    @ReqMapping("/loadUrgingOrganizeTree")
    @HasPermission("pay:charge:arrears:view")
    public Object loadUrgingOrganizeTree(String communityNo) {
        return chargeArrearsService.loadUrgingOrganizeTree(communityNo);
    }
}
